# -*- coding: utf-8 -*-#

# -------------------------------------------------------------------------------
# Name:         yingjiesheng_utils
# Description:  采集应届生网的数据
# Author:       zhuchunxu
# Date:         2020/11/6
# -------------------------------------------------------------------------------
import re
from logging import getLogger

from lxml import etree
from lxml.html import tostring

from spider_source.items import YingJieShengJob


class YingJieShengUtils:
    def __init__(self):
        self.logger = getLogger(__name__)

    def get_page_detail_links(self, response):
        """
        获取当前页所有的详情页链接
        """
        html = etree.HTML(response.text)
        job_tr_list = html.xpath("//table[@id='tb_job_list']/tbody/tr")
        cur_page_detail_link = []
        for tr in job_tr_list:
            hrefs = tr.xpath("./td/a/@href")
            if len(hrefs) > 0:
                cur_page_detail_link.append("http://my.yingjiesheng.com" + hrefs[0])

        return cur_page_detail_link

    def get_page_detail_info(self, response):
        """
        获取详情页内容
        """
        html = etree.HTML(response.text)
        if len(html.xpath("//div[@class='comtit']")) > 0:
            job = self.get_page_content_for_class_comtit(html, response)
        elif len(html.xpath("//div[@class='jobContent']")) > 0:
            job = self.get_page_content_for_class_container(html, response)

        return job

    def get_page_content_for_class_comtit(self, html, response):
        job = YingJieShengJob()
        try:
            job["title"] = html.xpath("//div[@class='comtit']/h1/a/text()")[0]
        except Exception as e:
            self.logger.error('提取职位名称错误')

        try:
            job["city"] = html.xpath("//div[@class='comtit']/p[1]/label[1]/span/a/text()")[0]
        except Exception as e:
            self.logger.error('提取工作城市错误')

        job["job_url"] = response.url
        try:
            job["catalog"] = html.xpath("//div[@class='comtit']/p[1]/label[4]/span/text()")[0]
        except Exception as e:
            self.logger.error('提取工作性质错误')

        try:
            job_tags = html.xpath("//div[@class='comtit']/p[@class='comtag']/a/text()")
            job["job_tag"] = ""
            if len(job_tags) > 0:
                job["job_tag"] = ",".join(job_tags)
        except Exception as e:
            self.logger.error('提取工作标签错误')

        try:
            job["job_desc"] = tostring(html.xpath("//div[@class='combox']")[0], encoding="utf-8").decode("utf-8")
        except Exception as e:
            self.logger.error('提取工作描述错误')

        try:
            job["job_company"] = html.xpath("//div[@class='sidebox']/div/a/span/text()")[0]
        except Exception as e:
            self.logger.error('提取公司名称错误')

        try:
            job["industry_company"] = \
                html.xpath("//div[@class='rdiv']/div[@class='sidebox'][1]/div[@class='inf']/label[1]/span/text()")[0]
        except Exception as e:
            self.logger.error('提取公司行业错误')

        try:
            job["scale_company"] = \
                html.xpath("//div[@class='rdiv']/div[@class='sidebox'][1]/div[@class='inf']/label[2]/span/text()")[0]
        except Exception as e:
            self.logger.error('提取公司规模错误')

        try:
            job["category_company"] = \
                html.xpath("//div[@class='rdiv']/div[@class='sidebox'][1]/div[@class='inf']/label[3]/span/text()")[0]
        except Exception as e:
            self.logger.error('提取公司性质错误')

        try:
            s = html.xpath("//div[@class='comtit']/p[1]/label[2]/span/text()")[0]
            data = re.findall(r"\d+\.?\d*", s)
            job["date"] = "{}-{}-{}".format(data[0], data[1], data[2])
        except Exception as e:
            self.logger.error('提取发布日期错误')

        return job

    def get_page_content_for_class_container(self, html, response):
        job = YingJieShengJob()
        job["title"] = ""
        try:
            job["city"] = html.xpath("//div[@id='container']/div/div/ol/li[2]/u/text()")[0]
        except Exception as e:
            self.logger.error('提取工作城市错误')

        try:
            job["date"] = html.xpath("//div[@id='container']/div/div/ol/li[1]/u/text()")[0]
        except Exception as e:
            self.logger.error('提取发布日期错误')

        try:
            job["catalog"] = html.xpath("//div[@id='container']/div/div/ol/li[3]/u/text()")[0]
        except Exception as e:
            self.logger.error('提取职位性质错误')
        try:
            job["job_tag"] = ",".join(html.xpath("//div[@id='container']/div/div[2]/a/text()"))
        except Exception as e:
            self.logger.error('提取职位标签错误')

        try:
            job["job_desc"] = tostring(html.xpath("//div[@class='jobIntro']")[0], encoding="utf-8").decode("utf-8")
        except Exception as e:
            self.logger.error('提取职位描述错误')
        job["job_url"] = response.url

        try:
            job["job_company"] = html.xpath("//div[@id='container']/div/h1/text()")[0]
        except Exception as e:
            self.logger.error('提取招聘单位错误')
        job["industry_company"] = ""
        job["scale_company"] = ""
        job["category_company"] = ""
        return job
